package com.abc.service.impl;

import com.abc.entity.Programme;
import com.abc.mapper.ProgrammeMapper;
import com.abc.mapper.UserMapper;
import com.abc.service.IProgrammeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2024-12-25
 */
@Service
public class ProgrammeServiceImpl extends ServiceImpl<ProgrammeMapper, Programme> implements IProgrammeService {
    @Autowired
    private JavaMailSender mailSender;

    private static final Logger logger = LoggerFactory.getLogger(ProgrammeServiceImpl.class);


    @Override
    public List<Programme> getProgrammesByUserId(Long userId) {
        return this.lambdaQuery().eq(Programme::getProgramUserName, userId).list();
    }

    @Override
    public boolean addProgramme(Programme programme, Long userId) {
        programme.setProgramUserName(userId);
        programme.setStatus("存在");
        programme.setGmtCreate(LocalDateTime.now());
        return this.save(programme);
    }

    @Override
    public boolean deleteProgramme(Integer programmeId, Long userId) {
        Programme programme = this.getById(programmeId);
        if (programme.getProgramUserName().equals(userId)) {
            programme.setStatus("删除");
            return this.updateById(programme);
        } else {
            return false;
        }
    }

    @Override
    public boolean updateProgramme(Programme programme, Long userId) {
        Programme existProgramme = this.getById(programme.getProgrammeId());
        if (existProgramme.getProgramUserName().equals(userId)) {
            existProgramme.setContent(programme.getContent());
            existProgramme.setProgrammeTime(programme.getProgrammeTime());
            existProgramme.setGmtModified(LocalDateTime.now());
            return this.updateById(existProgramme);
        } else {
            return false;
        }
    }

    @Override
    public boolean publishProgramme(Programme programme, String[] userEmails) {
        // 模拟发送邮件通知
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("日程通知");
        message.setText("您有一条新的日程：" + programme.getContent());
        for (String userEmail : userEmails) {
            message.setTo(userEmail);
            // 使用日志模拟发送邮件
            logger.info("模拟发送邮件给: {}", userEmail);
            logger.info("邮件主题: {}", message.getSubject());
            logger.info("邮件内容: {}", message.getText());
            // 实际发送邮件
//            mailSender.send(message);
        }
        return true;
    }

    @Override
    public List<Programme> getProgrammesToRemind() {
        // 查询需要提醒的日程（例如：日程时间在当前时间之后的）
        LocalDateTime now = LocalDateTime.now();
        return this.lambdaQuery()
                .ge(Programme::getProgrammeTime, now) // 查询 programme_time >= 当前时间
                .eq(Programme::getStatus, "存在") // 状态为“存在”
                .list();
    }
}
